<link rel="stylesheet" type="text/css" href="../static/Huploadify-master/Huploadify.css">
<script type="text/javascript" src="../static/Huploadify-master/jquery.Huploadify.js"></script>
<style>
    li {
        list-style-type: none;
        vertical-align: middle;
        height: 30px;
        line-height: 30px;
        margin: 10px 0;
    }
    li a{text-decoration:none;}
    li a img{
        border:1px solid #999999;
        margin-right: 20px;
        float: left
    }
    li a div{
        float: left;
        width: 50%;
        text-align: center;
        height: 30px;
        line-height: 30px;
        vertical-align: middle;
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
    }
    li a div{
        width:50%;
        text-align: center;
    }

</style>
<script type="text/javascript">

    //设置附件名称，附件url地址
    function setUploadParams(){
        var annex = "";
        var annexUrl = "";
        $("#attachment li").each(function () {
            annex += this.title + ",";
            annexUrl += this.id + ",";
        });
        if(annex.length > 0){
            $("#annex").val(annex.substring(0,annex.length - 1));
        }
        if(annexUrl.length > 0){
            $("#annexUrl").val(annexUrl.substring(0,annexUrl.length - 1));
        }
    }

    function xlget () {
        //20180517修改选择金融代码函数
        $('#company').inputgrid({
            idField: 'o.code',
            textField: 'o.name',
            searchField: ['o.code', 'o.name'],//查询字段多字段匹配
            columns: [[
                {field: 'o.code', title: '金融代码'},
                {field: 'o.name', title: '名称'},
                {field: 'o.address', title: '地址'}
            ]],
            _page: parent.page_list,
            _append: [],//隐藏列
            _table: "company as o",
            _db: "slave"
        });
    }

    $(function () {
        $("#btnSave").click(function () {
            if (!checkForm(document.getElementById("saveForm"))) {
                return;
            }

            var extObjUrl = $("#externalUrl").val();
            if (extObjUrl.length > 0) {
                //判断URL地址的正则表达式为:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
                var expressionURL = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
                var objExp = new RegExp(expressionURL);
                if (objExp.test(extObjUrl) != true) {
                    addError("externalUrl", "网址格式不正确");
                    return;
                }
            }

            setUploadParams();

            $("#btnSave").blur();
            showLoading();

            var fileInputs = $("#saveForm").find("input[type='file']").attr("type","hidden");

            fy_api.ajaxSubmit($("#saveForm"),{
                success: function (data) {

                    fileInputs.attr("type","file");

                    hideLoading();
                    if (!isSuccess(data)) {
                        $.messager.alert('系统提示', data.msg, 'error', function () {
                            self.focus();
                        });
                    } else {
                        parent.grid_get("grid").datagrid("load");
                        turnBack();
                    }
                }
            });
        });
        var keyId = (getUrlParam("keyId", url));
        if(keyId != ""){
            //编辑
            fy_api.load_template({
                controller: $("#saveForm").attr("templateParam", "keyId=" + keyId), loading: true, initValue: true, loaded: function(){
                    loaded(keyId);
                }
            });
        }else{
            //添加
            $("#divEdit").remove();
            fy_api.load_template({
                controller: $("#saveForm").attr("templateParam", "keyId=" + keyId), loading: true,initValue: true, loaded: function(){
                    loaded(keyId);
                    $("#saveForm").attr("action","news/save.do")
                }
            });
        }



    });
    function loaded(keyId){
        xlget ();
        loadFileList();
        //上传文件控件初始化
        fileUploadInit();
    }
    function turnBack() {
        parent.j_closeWin();
    }

    function fileUploadInit(){
        //定义上传文件类型
        var fileUploadType = "uploadfile";
        //保存文件到服务器上的路径
        var fileSavePath = saveBasePathInServer + "message/";
        $('#fileUpload').Huploadify({
            auto:false,
            fileTypeExts:'*.*',
            multi:true,
            formData:{savePath:fileSavePath},
            fileSizeLimit:10200,
            showUploadedPercent:false,
            showUploadedSize:false,
            removeTimeout:1000,
            uploader:uploadServer + fileUploadType + "&savePath=" + fileSavePath,
            method:'post',
            onUploadStart:function(file){},
            onInit:function(obj){},
            onUploadSuccess:function(file,data){
                data = eval("(" + data + ")");
                if(data.state == "SUCCESS"){
                    var fileName = file.name;
                    var fileUrl = lookingServer + data.url;
                    //如果是图片文件则显示图片，否则显示文件的连接
                    if(data.type == ".zip"){
                        $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                            "<a target='_blank' href='" + fileUrl + "'>" +
                            "<img src='../static/images/zipPic.png' height='30px' width='30px' style='margin-right:20px;' />" +
                            "<div>" + fileName + "</div>" +
                            "</a>" +
                            "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                            "</li>");
                    }else{
                        $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                            "<a target='_blank' href='" + fileUrl + "'>" +
                            "<img src='" + fileUrl + "' height='30px' width='30px' style='margin-right:20px;' />" +
                            "<div>" + fileName + "</div>" +
                            "</a>" +
                            "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                            "</li>");
                    }
                    if($("#attachment").find("li").length > 1){
                        $("#attachment").children().first().remove();
                    }
                }else{
                    j_tip("[" + file.name + "]" + data.state,"error");
                }
            },
            onUploadComplete:function(file){},
            onCancel:function(file){},
            onClearQueue:function(queueItemCount){},
            onDestroy:function(){},
            onSelect:function(file){},
            onQueueComplete:function(queueData){}
        });
    }

    //图片鼠标移入事件
    function imgOver(obj){
        $(obj).children("span").show();
    }
    //图片鼠标移出事件
    function imgOut(obj){
        $(obj).children("span").hide();
    }
    //删除图片
    function imgDel(obj){
        if(confirm("确定删除此图片吗?", "提示")){
            $(obj).parent().remove();
        }
    }

    function loadFileList(){
        var fileNames = $("#annex").val();
        var fileUrls = $("#annexUrl").val();
        if(fileUrls.length > 0){
            for(var i = 0; i < fileUrls.split(",").length; i++){
                var fileUrl =  fileUrls.split(",")[i];
                var fileName = fileNames.split(",")[i];
                if(fileUrl.substr(fileUrl.length-4) == ".zip"){
                    $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                        "<a target='_blank' href='" + fileUrl + "'>" +
                        "<img src='../static/images/zipPic.png' height='30px' width='30px' style='margin-right:20px;' />" +
                        "<div>" + fileName + "</div>" +
                        "</a>" +
                        "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                        "</li>");
                }else{
                    $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                        "<a target='_blank' href='" + fileUrl + "'>" +
                        "<img src='" + fileUrl + "' height='30px' width='30px' style='margin-right:20px;' />" +
                        "<div>" + fileName + "</div>" +
                        "</a>" +
                        "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                        "</li>");
                }
            }
        }
    }

    function btnChange(values){
        $("#companyName").val(values);
    }


</script>

<form action="news/update.do" method="post" id="saveForm" templateUrl="news/get.do">
    <div id="divEdit">
        <input type="hidden" name="id" value="${id}"/>
        <input type="hidden" name="deleteFlag" value="${deleteFlag}"/>
    </div>
    <table style="margin: 10px auto;border: 1px solid darkgrey;padding: 10px">
        <tr>
            <td>
                <div class="div-label"><span class="nullTip">*</span>标题：</div>
            </td>
            <td>
                <div class="div-item">
                    <input type="text" isNull name="title" value="${title}" />
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label">公司名称：</div>
            </td>
            <td>
                <div class="div-item">
                    <input name="code" type="hidden" id="code"  value="${code}" clear/>
                    <input type="text" name="companyName" id="company" value="${companyName}"/>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label">是否在主页使用：</div>
            </td>
            <td>
                <div class="div-item">
                    <select name="tipIndexFlag" id="tipIndexFlag" style="width: 160px;" value="${tipIndexFlag}" >
                        <option value="1" >启用</option>
                        <option value="0" >禁用</option>
                    </select>
                </div>
            </td>
        </tr>
        <tr >
            <td>
                <div class="div-label">内容：</div>
            </td>
            <td >
                <div >
                    <textarea name="context" rows="15" cols="50" id="context">${context}</textarea>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label">外网URL：</div>
            </td>
            <td colspan="3">
                <div class="div-item" style="width: 90%">
                    <input type="text" name="externalUrl" id="externalUrl" value="${externalUrl}"/>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label">附件：</div>
            </td>
            <td>
                <div id="fileUpload"></div>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <ul id="attachment" style="padding:5px"></ul>
            </td>
        </tr>
    </table>
    <!--附件名-->
    <input type="hidden" value="${annex}" name="annex" id="annex"/>
    <!--附件上传到服务器后的保存路径-->
    <input type="hidden" value="${annexUrl}" name="annexUrl" id="annexUrl"/>
</form>
<div class="form-buttons">
    <a id="btnSave" class="easyui-linkbutton" iconCls="icon-save">保存</a>
    <a id="btnBack" class="easyui-linkbutton c2" iconCls="icon-undo" onClick="turnBack();">返回</a>
</div>